import React, { useState, useEffect } from 'react';
import { obj_str, for_obj, cline, uReal, ErrView, Wait, BaseDlg, NuiBt } from 'sui';
import { g_db } from 'db/state';
import { loc } from 'local';
import { get_rgb, split_psn } from 'client/pick/cmd';
import { put_log } from 'base/ctl/log';
import { Scan, Pcode} from './ctl';

const DlgFind = ({ open, setOpen, cur }) => {
    const [fin, setFin] = useState("scan");
    const [err, setErr] = useState("");
    const [value, SetValue] = useState();
    const [wait, setWait] = useState(false);

    useEffect(() => {
        if (!open) {
            return;
        }

        setErr("");
        SetValue(null);

        g_db.stop_scan();
    }, [open]);

    const hfind = (msn) => {
        if (!msn) {
            return;
        }

        put_log("输入编码", msn, "查找");

        setWait(true);

        uReal(null, "pair/load",
            {
                line: cline(),
                k: "box",
            },
            r => {
                let find = null;

                for_obj(r, (box, v) => {
                    if (find) {
                        return;
                    }

                    const attr = v["attr"];
                    if (obj_str(attr, "tp") !== cur) {
                        return;
                    }

                    const tmp = v[msn];
                    if (tmp) {
                        find = tmp;
                        find["box_sn"] = box;
                        find["msn"] = msn;
                    }
                });

                if (find) {
                    SetValue(find);
                    setErr("");
                } else {
                    SetValue(null);
                    setErr("没有找到");
                }

                setWait(false);
            },
            e => {
                setWait(false);
                setErr("没有找到");
            });
    };

    const getView = () => {
        if (fin === "scan") {
            return <Scan hcmd={hfind} open={open} setOpen={setOpen} lok="搜索"/>;
        }

        return <Pcode hcmd={v => {
            put_log("编码", v, "手工录入");

            if (v.length === 8) {
                hfind(`${v[0]}${v[1]}${v[2]}${v[5]}${v[3]}${v[6]}${v[4]}${v[7]}`);
            } else if (v.length === 7) {
                hfind(`${v[0]}${v[1]}${v[2]}0${v[3]}${v[5]}${v[4]}${v[6]}`);
            } else if (v.length === 6) {
                hfind(`${v[0]}${v[1]}${v[2]}0${v[3]}0${v[4]}${v[5]}`);
            } else {
                hfind(v);
            }
        }} open={open} setOpen={setOpen} lok="搜索"/>;
    };

    const getTitle = () => {
        if (cur === "outer") {
            return "查找外圈轴承";
        } else if (cur === "inner") {
            return "查找内圈轴承";
        }
    };

    const getResult = () => {
        if (err) {
            return <ErrView err={err}/>;
        }

        if (!value) {
            return;
        }

        const sn = obj_str(value, "box_sn");
        const cl = get_rgb(sn);

        return (
            <div className="text-2xl mx-4 mb-4">
                <div className="rounded-lg px-4" style={{ background: `rgb(${cl[0]}, ${cl[1]}, ${cl[2]})`, color: `${cl[3]}` }}>
                    箱号<span className="ml-4">{sn}</span>
                </div>
                <div>位置<span className="ml-4">{obj_str(value, "pos")}</span></div>
                <div>轴承<span className="ml-4">{split_psn(obj_str(value, "msn"))}</span></div>
            </div>
        );
    };

    return (
        <BaseDlg open={open} title={getTitle()}>
            <div className="flex flex-col">
                {getResult()}
                <div className="flex flex-row ml-4">
                    <NuiBt color={fin === "scan" ? "green": "blue"} hcmd={() => setFin("scan")}>{loc("base_62")}</NuiBt>
                    <NuiBt className="ml-4" color={fin === "scan" ? "blue": "green"} hcmd={() => setFin("pass")}>{loc("base_59")}</NuiBt>
                </div>
                {getView()}
                <Wait wait={wait}/>
            </div>
        </BaseDlg>
    );
};

export {DlgFind};
